
import mysql.connector
import numpy as np
from mysql.connector import Error
import pandas as pd

def LookupLW(submaterial,impedance,trace):
    try:
        mydb = mysql.connector.connect(

            host="bmckean.dynu.net",
            user="msurf-script",
            passwd="Capstone#2019",
            database="metasurface_design"

        )
        mycursor = mydb.cursor()
    except Error as e:
        print(e)
    try:
        query = "SELECT * from dogbone_simulations;"
        mycursor.execute(query)
        results = mycursor.fetchall()
        mydb.commit()
        tab_name = "dogbone_simulations"
        query = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = \"%s\";" % (tab_name)
        mycursor.execute(query)
        colsqur = mycursor.fetchall()
        cols = []
        for col in colsqur: # just to get the coumn names
            col = str(col[0])
            cols.append(col)
        mydb.commit()
    except Error as e:
        print("Error reading data from MySQL table", e)
    results=pd.DataFrame(results)
    results.columns = cols ## appenbd the queried column names
    results=results[results['substrate_material']== submaterial] # only filter for the subtrate in question
    results=results[results['location']==trace] ## only use specific trace, i.e. top mid bot
    closestRes=(results.iloc[(results['z'] - impedance).abs().argsort()[:1]]) # get closest value to our impedance in question
    print(impedance)
    print(closestRes)
    return float(closestRes['width']),float(closestRes['length'])



